<?php
namespace App\Services\PingAnPay;

use App\Services\PingAnPay\lib\pingan;

/**
 * 平安银行退款查询
 *
 * @author   Leen
 * @date     2016/08/1
 */

    const SEND_URL = 'https://testebank.sdb.com.cn/corporbank/KH0006.pay';

class RefundQuery {



    /**
     * 退款订单状态查询
     * @param  string $value [description]
     * @return [type]        [description]
     */
    /**
     * 单笔订单状态查询
     * @param  [type] $start_time   [查询开始时间（退款请求接收时间）YYYYMMDDHHMMSS]
     * @param  [type] $end_time     [查询结束时间（退款请求接收时间）YYYYMMDDHHMMSS]
     * @return [type]               [description]
     */
    // public function singleOrderRefundQuery($order_id, $pay_num, $refund_amt, $reason = '', $remark = '')
    public function singleOrderRefundQuery($start_time, $end_time)
    {

        $pingan = new pingan();

		$data = array(
			'masterId' => config('pinganpay.masterId'),
            'beginDate' => $start_time,
            'endDate' => $end_time,
		);


		$xml_data = $pingan->array_to_xml($data);
        $merchantCertFile = config('pinganpay.merchant_cert_file');

		//获取签名后的orig和sign
		$orig = $pingan->getOrig($xml_data);
		$sign = $pingan->getSign($merchantCertFile, $xml_data);

		//通过curl请求接口
		$parms = 'orig=' . $orig . '&sign=' . $sign;
		$rsponse = $pingan->curl(SEND_URL, $parms);

		//通过字符串截取获取orig
		$rsponseData = explode('orig=', $rsponse);
		$rsponse2Data = explode('SDBPAYGATE=', $rsponseData[1]);

		//解码
		$formOrig = $pingan->_base64_url_decode($rsponse2Data[0]);

		$xml_dom = str_replace("GBK", "utf-8", $formOrig);
		$xml_dom = mb_convert_encoding($xml_dom, 'utf-8', 'gbk');
		$result = $pingan->xml_to_array($xml_dom);

		return $result;
    }


}

